﻿using System;
using System.Linq;
using System.Text;
using Object = ArtheaEngine.Model.Object;
using System.Collections.Generic;
using System.Configuration;

namespace ArtheaEngine.Model
{
    public partial class World : IEntity, ICloneable
    {
        public World()
        {
            Context = new ArtheaModelContainer();
            Characters = new HashSet<Character>();
            Objects = new HashSet<Object>();
            Connections = new HashSet<IConnection>();
        }

        // Explicit interface method impl -- available for 
        // clients of ICloneable, but invisible to casual 
        // clients of MyCloneableClass
        object ICloneable.Clone()
        {
            // simply delegate to our type-safe cousin
            return this.Clone();
        }

        // Friendly, type-safe clone method
        public virtual World Clone()
        {
            // Start with a flat, memberwise copy
            World x = new World();

            x.Id = Id;
            x.Name = Name;
            x.Port = Port;

            return x;
        }
        public ArtheaModelContainer Context { get; private set; }

        public ICollection<IConnection> Connections { get; private set; }

        public ICollection<Character> Characters
        {
            get; private set;
        }

        public ICollection<Object> Objects 
        {
            get; private set;
        }

        public Room GetDefaultRoom()
        {
            var room = Context.Rooms.FirstOrDefault();

            if (room != null) return room;

            room = new Room { Name = "Limbo", Description = "You are in darkness.", Terrain = Terrain.Air };

            room.Area = GetDefaultArea();

            return room;
        }

        public Area GetDefaultArea()
        {
            var area = Context.Areas.FirstOrDefault(a => a.Name == "Limbo");

            if (area != null) return area;

            area = new Area { Name = "Limbo", Credits = "Arthea", World = this };

            return area;
        }
    }
}
